package hz.mobileproject.pda.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.response.Response_familyPhone_item;
import hz.mobileproject.pda.entity.response.Rp_loadEvaluationType;

/**
 * Created by Lc on 2017/11/22.
 */

public class FamilyPhoneEvaluateDialog extends Dialog implements View.OnClickListener {
    private Button btn_positive;
    private Button btn_nagetive;
    private EditText et_remark;
    private TextView tv_select_evaluate;
    private OnPositiveBtnClick onPositiveBtnClick;

    private String mEvaluate;
    private String mRemark;
    private List<Rp_loadEvaluationType> loadEvaluationTypeList;
    private List<String> stringList;
    //当前选择的评价项
    private Rp_loadEvaluationType curEvaluateType;

    public FamilyPhoneEvaluateDialog(@NonNull Context context, List<Rp_loadEvaluationType> list,
                                     String evaluate,String remark) {
        super(context, R.style.toumingDialog);
        this.loadEvaluationTypeList = list;
        this.mEvaluate = evaluate;
        this.mRemark = remark;
        this.stringList = new ArrayList<>();
        if(loadEvaluationTypeList != null){
            for(Rp_loadEvaluationType let : loadEvaluationTypeList){
                stringList.add(let.getValue());
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_family_phone_ecaluate);
        btn_positive = (Button) findViewById(R.id.btn_positive);
        btn_nagetive = (Button) findViewById(R.id.btn_nagetive);
        tv_select_evaluate = (TextView) findViewById(R.id.tv_select_evaluate);
        et_remark = (EditText) findViewById(R.id.et_remark);
        btn_positive.setOnClickListener(this);
        btn_nagetive.setOnClickListener(this);
        tv_select_evaluate.setOnClickListener(this);
        if(!StringUtils.isEmpty(mEvaluate)){
            tv_select_evaluate.setText(mEvaluate);
            for(int i=0;i<loadEvaluationTypeList.size();i++){
                if(mEvaluate.equals(loadEvaluationTypeList.get(i).getValue())){
                    curEvaluateType = loadEvaluationTypeList.get(i);
                    break;
                }
            }
        }
        if(!StringUtils.isEmpty(mRemark)){
            et_remark.setText(mRemark);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_positive:
                if(onPositiveBtnClick != null){
                    if(curEvaluateType==null || "请选择".equals(tv_select_evaluate.getText())){
                        ToastUtils.showLong("未选择评价");
                        return;
                    }
                    onPositiveBtnClick.onClick(curEvaluateType,et_remark.getText().toString());
                    this.dismiss();
                }
                break;
            case R.id.btn_nagetive:
                this.dismiss();
                break;
            case R.id.tv_select_evaluate:
                View contentView = LayoutInflater.from(getContext()).inflate(R.layout.pop_custom_list,null);
                //处理popWindow 显示内容
                ListView listView = (ListView) contentView.findViewById(R.id.lv_dialog);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),R.layout.item_pop_inlv, stringList);
                listView.setAdapter(adapter);
                //创建并显示popWindow
                final CustomPopWindow mListPopWindow= new CustomPopWindow.PopupWindowBuilder(getContext())
                        .setView(contentView)
                        .size(v.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT)
                        .create()
                        .showAsDropDown(v,0,0);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        mListPopWindow.dissmiss();
                        tv_select_evaluate.setText(stringList.get(position));
                        curEvaluateType = loadEvaluationTypeList.get(position);
                    }
                });
                break;
        }
    }

    public interface OnPositiveBtnClick{
        void onClick(Rp_loadEvaluationType curEvaluateType, String remark);
    }

    public void setOnPositiveBtnClick(OnPositiveBtnClick onPositiveBtnClick) {
        this.onPositiveBtnClick = onPositiveBtnClick;
    }
}
